// d_init include common
setViewDistance GVAR(InitialViewDistance);

if (GVAR(WithAcre)) then {
	[GVAR(AcreSignalLoss) / 100] call acre_api_fnc_setLossModelScale; // there is /100 as conversion from %, because propper value is from 0.0 to 1.0, but "description.ext" does not support float numbers, so value in params is percentual
	if (GVAR(AcreRadioOnBackWorks) == 1) then {
		["onBack", true] call acre_api_fnc_toggleAceFeatureCompat
	};
};

GVAR(last_target_idx) = -1;
GVAR(target_names) = [];
for "_i" from 0 to 100000 do {
	_ar = [];
	_mname = format [QGVAR(target_%1), _i];
	_dtar = __getMNsVar2(_mname);
	if (isNil "_dtar") exitWith {
		GVAR(last_target_idx) = _i - 1;
	};
	_dtar enableSimulation false;
	
	_name = GV(_dtar,GVAR(cityname));
	if (!isNil "_name") then {
		_pos = getPosASL _dtar;
		_pos set [2, 0];
		_ar set [count _ar, _pos]; // position CityCenter by logic
		if (isServer) then {
			_dtar setDir 0;
		};
		_ar set [count _ar, _name]; // name village/city
		_radius = GV(_dtar,GVAR(cityradius));
		_ar set [count _ar, if (isNil "_radius") then {300} else {_radius}];
	} else {
		_nlocs = nearestLocations [getPosASL _dtar, ["NameCityCapital","NameCity","NameVillage"], 1000];
		if (count _nlocs > 0) then {
			_nl = nearestLocations [locationPosition (_nlocs select 0), ["CityCenter"], 1000];
			_pos = if (count _nl > 0) then {
				locationPosition (_nl select 0)
			} else {
				locationPosition (_nlocs select 0)
			};
			_pos set [2, 0];
			_ar set [count _ar, _pos]; // position CityCenter
			if (isServer) then {
				_dtar setDir 0;
				_dtar setPos _pos;
			};
			_name = text (_nlocs select 0);
			_ar set [count _ar, _name]; // name village/city
			_radius = GV(_dtar,GVAR(cityradius));
			_ar set [count _ar, if (isNil "_radius") then {300} else {_radius}];
			_dtar setVariable [QGVAR(cityname), _name];
		} else {
			hint ("No city found near target location " + _mname);
		};
	};
	__TRACE_1("All targets found","_ar")
	GVAR(target_names) set [count GVAR(target_names), _ar];
};

#ifdef __DEBUG__
// only for debugging, creates markers at all main target positions
// {
	// _pos = _x select 0;
	// _name = _x select 1;
	// _size = _x select 2;
	// _marker= createMarkerLocal [_name, _pos];
	// _marker setMarkerShapeLocal "ELLIPSE";
	// _name setMarkerColorLocal "ColorGreen";
	// _name setMarkerSizeLocal [_size,_size];
	// _name = _name + "xx";
	// _marker= createMarkerLocal [_name, _pos];
	// _marker setMarkerTypeLocal "mil_dot";
	// _name setMarkerColorLocal "ColorBlack";
	// _name setMarkerSizeLocal [0.5,0.5];
	// _name setMarkerTextLocal _name;
// } forEach GVAR(target_names);
#endif

if (isServer) then {
	#ifdef __OWN_SIDE_INDEPENDENT__
	GVAR(FLAG_BASE) setflagtexture "\a3\data_f\flags\flag_green_co.paa";
	#endif
	#ifdef __OWN_SIDE_BLUFOR__
	GVAR(FLAG_BASE) setflagtexture "pics\ftblufor.paa";
	#endif
	#ifdef __OWN_SIDE_OPFOR__
	GVAR(FLAG_BASE) setflagtexture "pics\ftopfor.paa";
	#endif
};
 
GVAR(FLAG_BASE) allowDamage false;

// positions for aircraft factories (if one get's destroyed you're not able to service jets/service choppers/repair wrecks)
// first jet service, second chopper service, third wreck repair

#ifndef __TT__
_mpos = markerPos QGVAR(base_jet_fac);
if (str(_mpos) != "[0,0,0]") then {
	_mpos set [2,0];
	_mpos2 = markerPos QGVAR(base_chopper_fac);
	_mpos2 set [2,0];
	_mpos3 = markerPos QGVAR(base_wreck_fac);
	_mpos3 set [2,0];
	GVAR(aircraft_facs) = [[_mpos, markerDir QGVAR(base_jet_fac)],[_mpos2, markerDir QGVAR(base_chopper_fac)],[_mpos3, markerDir QGVAR(base_wreck_fac)]];
	deleteMarker QGVAR(base_jet_fac);
	deleteMarker QGVAR(base_chopper_fac);
	deleteMarker QGVAR(base_wreck_fac);
} else {
	GVAR(aircraft_facs) = [];
};
#else
GVAR(aircraft_facs) = [];
#endif

// TYRGHEN - made auto adaptating code.
#ifndef __TT__
_count = 1;
__TRACE(markerType format["INDESTRUCTIBLE%1",_count])
while { markerType format["INDESTRUCTIBLE%1",_count] != "" && _count < 20} do {
	_marker = format["INDESTRUCTIBLE%1",_count];
	_marker setMarkerAlpha 0;
	_houses = nearestObjects[(markerPos _marker),["House","House_F"],((markerSize _marker) select 0)];
	__TRACE_1(_marker,"_houses")
	{ _x addEventHandler ["handleDamage", {0}];
	  _x allowDamage false;
	} foreach _houses;
	__INC(_count);
};
GVAR(FLAG_BASE) addEventHandler ["handleDamage", {0}];
GVAR(FLAG_BASE) allowDamage false;
#else
//AMMOBUILDING2 addEventHandler ["handleDamage", {0}];
#endif

// position base, a,b, for the enemy at base trigger and marker
#ifdef __DEFAULT__
_mpos = markerPos QGVAR(base_marker);
_mpos set [2,0];
_hasbasemarker = str(_mpos) != "[0,0,0]";
QGVAR(base_marker) setMarkerAlphaLocal 0;
_msize = markerSize QGVAR(base_marker);
GVAR(base_array) = [_mpos, _msize select 0, _msize select 1, markerDir QGVAR(base_marker)];
#endif
#ifdef __TT__
_mpos = markerPos QGVAR(base_marker_w);
_mpos set [2,0];
_mpos2 = markerPos QGVAR(base_marker_e);
_mpos2 set [2,0];
_hasbasemarker = (str(_mpos) != "[0,0,0]") && (str(_mpos2) != "[0,0,0]");
QGVAR(base_marker_w) setMarkerAlphaLocal 0;
QGVAR(base_marker_e) setMarkerAlphaLocal 0;
_msize = markerSize QGVAR(base_marker_w);
_msize2 = markerSize QGVAR(base_marker_e);
GVAR(base_array) = [
	[_mpos, _msize select 0, _msize select 1, markerDir QGVAR(base_marker_w)], // blufor
	[_mpos2, _msize2 select 0, _msize2 select 1, markerDir QGVAR(base_marker_e)] // opfor
];
#endif

// position of radar and anti air at own base
#ifdef __DEFAULT__
_mpos = markerPos QGVAR(base_radar_pos);
_mpos set [2,0];
GVAR(base_radar_pos) = _mpos;
_mpos = markerPos QGVAR(base_anti_air1);
_mpos set [2,0];
GVAR(base_anti_air1) = _mpos;
_mpos = markerPos QGVAR(base_anti_air2);
_mpos set [2,0];
GVAR(base_anti_air2) = _mpos;
#endif

QGVAR(isledefense_marker) setMarkerAlphaLocal 0;
